package handler

import (
	"essay/api/request"
	"essay/internal/dao"
	"essay/internal/models"
	"github.com/gin-gonic/gin"
)

func CreateLike(c *gin.Context) {
	var req request.CreateLikeReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var like models.Likes
	dao.GetOneByFields(&models.Likes{UserId: uint64(c.GetUint("userId"))}, &like)
	var essay models.Essays
	dao.GetOneByFields(&models.Essays{Id: req.EssayId}, &essay)
	if essay.Id == 0 {
		c.JSON(200, gin.H{
			"code": 10002,
			"msg":  "文章不存在",
			"data": nil,
		})
		return
	}
	likes := models.Likes{
		UserId:  uint64(c.GetUint("userId")),
		EssayId: req.EssayId,
		Live:    req.Live + 1,
	}
	if !dao.Create(&likes) {
		c.JSON(200, gin.H{
			"code": 10003,
			"msg":  "点赞失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "点赞成功",
		"data": likes,
	})
}

func CreateLikeComment(c *gin.Context) {
	var req request.CreateLikeCommentReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var LikeComment models.LikeComment
	dao.GetOneByFields(&models.LikeComment{UserId: uint64(c.GetUint("userId"))}, &LikeComment)
	var comment models.Comment
	dao.GetOneById(uint(req.CommentId), &comment)
	if comment.Id == 0 {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "评论不存在",
			"data": nil,
		})
		return
	}
	likeComment := models.LikeComment{
		UserId:    uint64(c.GetUint("userId")),
		CommentId: req.CommentId,
		Live:      req.Live + 1,
	}
	if !dao.Create(&likeComment) {
		c.JSON(200, gin.H{
			"code": 10003,
			"msg":  "点赞失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "点赞成功",
		"data": likeComment,
	})

}

func CreateLikeAnswerComment(c *gin.Context) {
	var req request.CreateLikeAnswerCommentReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var answerComment models.LikeAnswerComment
	dao.GetOneByFields(&models.LikeAnswerComment{UserId: uint64(c.GetUint("userId"))}, &answerComment)
	var answer models.AnswerComment
	dao.GetOneById(uint(req.AnswerCommentId), &answer)
	if answer.Id == 0 {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "回复评论不存在",
			"data": nil,
		})
		return
	}
	comment := models.LikeAnswerComment{
		UserId:          uint64(c.GetUint("userId")),
		AnswerCommentId: req.AnswerCommentId,
		Live:            req.Live + 1,
	}
	if !dao.Create(&comment) {
		c.JSON(200, gin.H{
			"code": 10003,
			"msg":  "点赞失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "点赞成功",
		"data": comment,
	})
}

func DeleteLikeComment(c *gin.Context) {
	var req request.DeleteLikeCommentReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"date": err.Error(),
		})
		return
	}
	var like models.LikeComment
	dao.GetOneById(uint(req.Id), &like)
	if like.Id == 0 {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "点赞记录不存在",
			"data": nil,
		})
		return
	}
	comment := models.LikeComment{Id: uint64(req.Id)}
	if !dao.Delete(&comment) {
		c.JSON(200, gin.H{
			"code": 10002,
			"msg":  "删除失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
		"data": nil,
	})
}

func DeleteLikeAnswerComment(c *gin.Context) {
	var req request.DeleteLikeAnswerCommentReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"date": err.Error(),
		})
		return
	}
	var like models.LikeAnswerComment
	dao.GetOneById(uint(req.Id), &like)
	if like.Id == 0 {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "点赞记录不存在",
			"data": nil,
		})
		return
	}
	comment := models.LikeComment{Id: uint64(req.Id)}
	if !dao.Delete(&comment) {
		c.JSON(200, gin.H{
			"code": 10002,
			"msg":  "删除失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
		"data": nil,
	})
}
